package com.gc.gc.role.service.impl;


import com.gc.gc.mybatis.entity.PtRoleEntity;
import com.gc.gc.mybatis.flexigrid.PadingRstType;
import com.gc.gc.mybatis.flexigrid.PagingBean;
import com.gc.gc.mybatis.ztree.ZtreeEntity;
import com.gc.gc.role.entity.SearchRoleEntity;
import com.gc.gc.role.repository.RoleDao;
import com.gc.gc.role.service.RoleService;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleDao roleDao;

    @Override
    public PadingRstType<PtRoleEntity> getRoleInfoListByPage(SearchRoleEntity search, PagingBean paging) {
        paging.deal(PtRoleEntity.class);
        PadingRstType<PtRoleEntity> padingRstType = new PadingRstType<>();
        padingRstType.setPage(paging.getPage());
        List<PtRoleEntity> list = roleDao.queryRoleListByPage(search,paging);
        Integer total = roleDao.queryRoleListTotal(search);
        padingRstType.setTotal(total);
        padingRstType.setRawRecords(list);
        padingRstType.putItems();

        return padingRstType;
    }

    @Override
    public List<ZtreeEntity> getMenuZtreeListByRoleId(String roleUuid) {
        Locale locale = LocaleContextHolder.getLocale();
        List<ZtreeEntity> list = roleDao.getMenuZtreeList(locale.toString());
        List<String> menuIdList = roleDao.getMenuZtreeListByRoleId(roleUuid);
        for(ZtreeEntity item:list){
            for(String menuId :menuIdList){
                if(menuId.equals(item.getId())){
                    item.setChecked(true);
                }
            }
        }

        return list;
    }

    @Override
    public void saveMenuByRoleId(String roleUuid, String menuIds) {
        String[] menuArray = menuIds.split(",");
        roleDao.deleteMenuByRole(roleUuid);
        roleDao.insertMenuRefRole(roleUuid,menuArray);

    }

    @Override
    public void deleteRoleByRoleId(String roleUuid) {
            roleDao.deleteRoleById(roleUuid);

    }
}